# favorites_page.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class FavoritesPage:
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://example.com/favorites'
        self.add_to_favorites_button_locator = (By.ID, 'add-to-favorites-button')  # 添加到收藏按钮定位器
        self.remove_from_favorites_button_locator = (By.ID, 'remove-from-favorites-button')  # 从收藏移除按钮定位器
        self.favorites_list_locator = (By.ID, 'favorites-list')  # 收藏列表定位器
        self.item_name_locator = (By.ID, 'item-name')  # 商品名称定位器
        self.item_description_locator = (By.ID, 'item-description')  # 商品描述定位器
        self.item_price_locator = (By.ID, 'item-price')  # 商品价格定位器
        self.item_image_locator = (By.ID, 'item-image')  # 商品图片定位器
        self.favorites_empty_message_locator = (By.ID, 'favorites-empty-message')  # 收藏为空消息定位器
        self.favorites_success_message_locator = (By.ID, 'favorites-success-message')  # 收藏成功消息定位器
        self.favorites_error_message_locator = (By.ID, 'favorites-error-message')  # 收藏错误消息定位器

    # 导航到收藏页面
    def navigate(self):
        self.driver.get(self.url)

    # 点击添加到收藏按钮
    def click_add_to_favorites_button(self):
        self.driver.find_element(*self.add_to_favorites_button_locator).click()

    # 点击从收藏移除按钮
    def click_remove_from_favorites_button(self):
        self.driver.find_element(*self.remove_from_favorites_button_locator).click()

    # 获取收藏列表
    def get_favorites_list(self):
        return self.driver.find_elements(*self.favorites_list_locator)

    # 获取商品名称
    def get_item_name(self):
        return self.driver.find_element(*self.item_name_locator).text

    # 获取商品描述
    def get_item_description(self):
        return self.driver.find_element(*self.item_description_locator).text

    # 获取商品价格
    def get_item_price(self):
        return self.driver.find_element(*self.item_price_locator).text

    # 获取商品图片
    def get_item_image(self):
        return self.driver.find_element(*self.item_image_locator).get_attribute('src')

    # 获取收藏为空的消息
    def get_favorites_empty_message(self):
        return self.driver.find_element(*self.favorites_empty_message_locator).text

    # 获取收藏成功消息
    def get_favorites_success_message(self):
        return self.driver.find_element(*self.favorites_success_message_locator).text

    # 获取收藏错误消息
    def get_favorites_error_message(self):
        return self.driver.find_element(*self.favorites_error_message_locator).text

    # 等待收藏操作完成
    def wait_for_favorites_operation_complete(self):
        WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.favorites_success_message_locator)
        )